home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / TURBOPASCAL WIN / DOCDEMOS.PAK / WORKERS.PAS < prev    next >
Pascal/Delphi Source File  |  1992-06-08  |  3KB  |  155 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo unit                                    }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit Workers;
  10.  
  11. interface
  12.  
  13. const
  14.   PayPeriods        =  26;       { per annum }
  15.   OvertimeThreshold =  80;       { per pay period }
  16.   OvertimeFactor    = 1.5;       { times normal hourly rate }
  17.  
  18. type
  19.   PEmployee = ^TEmployee;
  20.   TEmployee = object
  21.     Name: string[25];
  22.     Title: string[25];
  23.     Rate: Real;
  24.     constructor Init(AName, ATitle: String; ARate: Real);
  25.     destructor Done; virtual;
  26.     function GetName: String;
  27.     function GetPayAmount: Real; virtual;
  28.     function GetRate: Real;
  29.     function GetTitle: String;
  30.     procedure Show; virtual;
  31.   end;
  32.  
  33.   PHourly = ^THourly;
  34.   THourly = object(TEmployee)
  35.     Time: Integer;
  36.     constructor Init(AName, ATitle: String; ARate: Real; ATime: Integer);
  37.     function GetPayAmount: Real; virtual;
  38.     function GetTime: Integer;
  39.   end;
  40.  
  41.   PSalaried = ^TSalaried;
  42.   TSalaried = object(TEmployee)
  43.     function GetPayAmount: Real; virtual;
  44.   end;
  45.  
  46.   PCommissioned = ^TCommissioned;
  47.   TCommissioned = object(TSalaried)
  48.     Commission: Real;
  49.     SalesAmount: Real;
  50.     constructor Init(AName, ATitle: String;
  51.       ARate, ACommission, ASalesAmount: Real);
  52.     function GetPayAmount: Real; virtual;
  53.   end;
  54.  
  55.  
  56. implementation
  57.  
  58. function RoundPay(Wages: Real): Real;
  59. { Round pay amount to ignore any pay less than 1 penny }
  60. begin
  61.   RoundPay := Trunc(Wages * 100) / 100;
  62. end;
  63.  
  64.  
  65. { TEmployee }
  66. constructor TEmployee.Init(AName, ATitle: String; ARate: Real);
  67. begin
  68.   Name := AName;
  69.   Title := ATitle;
  70.   Rate := ARate;
  71. end;
  72.  
  73. destructor TEmployee.Done;
  74. begin
  75. end;
  76.  
  77. function TEmployee.GetPayAmount: Real;
  78. begin
  79.   RunError(211);         { Give runtime error }
  80. end;
  81.  
  82. function TEmployee.GetName: String;
  83. begin
  84.   GetName := Name;
  85. end;
  86.  
  87. function TEmployee.GetRate: Real;
  88. begin
  89.   GetRate := Rate;
  90. end;
  91.  
  92. function TEmployee.GetTitle: String;
  93. begin
  94.   GetTitle := Title;
  95. end;
  96.  
  97. procedure TEmployee.Show;
  98. var
  99.   S: string[25];
  100. begin
  101.   S := GetName;
  102.   Write(S, ' ':30 - Length(S));
  103.   S := GetTitle;
  104.   Write(S, ' ':30 - Length(S));
  105.   Writeln(GetPayAmount:10:2);
  106. end;
  107.  
  108. { THourly }
  109. constructor THourly.Init(AName, ATitle: String; ARate: Real; ATime: Integer);
  110. begin
  111.   TEmployee.Init(AName, ATitle, ARate);
  112.   Time := ATime;
  113. end;
  114.  
  115. function THourly.GetPayAmount: Real;
  116. var
  117.   OverTime: Integer;
  118. begin
  119.   Overtime := Time - OvertimeThreshold;
  120.   if Overtime > 0 then
  121.     GetPayAmount := RoundPay(OvertimeThreshold * Rate +
  122.       OverTime * OvertimeFactor * Rate)
  123.   else
  124.     GetPayAmount := RoundPay(Time * Rate);
  125. end;
  126.  
  127. function THourly.GetTime: Integer;
  128. begin
  129.   GetTime := Time;
  130. end;
  131.  
  132.  
  133. { TSalaried }
  134. function TSalaried.GetPayAmount: Real;
  135. begin
  136.   GetPayAmount := RoundPay(Rate / PayPeriods);
  137. end;
  138.  
  139.  
  140. { TCommissioned }
  141. constructor TCommissioned.Init(AName, ATitle: String;
  142.   ARate, ACommission, ASalesAmount: Real);
  143. begin
  144.   TSalaried.Init(AName, ATitle, ARate);
  145.   Commission := ACommission;
  146.   SalesAmount := ASalesAmount;
  147. end;
  148.  
  149. function TCommissioned.GetPayAmount: Real;
  150. begin
  151.   GetPayAmount := RoundPay(TSalaried.GetPayAmount + Commission * SalesAmount);
  152. end;
  153.  
  154. end.
  155.